Stored Procedures [dbo].[asi_DocumentCreateCopy]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@folderHierarchyKeyuniqueidentifier16
@keyuniqueidentifier16
@userKeyuniqueidentifier16
@loggedInUserGroupKeyuniqueidentifier16
@documentNamenvarchar(100)200
SQL Script
-- creates a copy of the document given by key in the folder given by FolderHierarchyKey
-- Key can be either a DocumentKey or a DocumentVersionKey.  If the latter, copies the
-- latest version available
-- IsSystem is defaulted to false when creating a copy even if copying an IsSystem=true item.
CREATE PROCEDURE [dbo].[asi_DocumentCreateCopy]
   @folderHierarchyKey uniqueidentifier,
   @key uniqueidentifier,
   @userKey uniqueidentifier,
   @loggedInUserGroupKey uniqueidentifier = '00000000-0000-0000-0000-000000000000', -- if this is empty, we assume the user is not logged in
   @documentName nvarchar(100) = null
AS
BEGIN
   DECLARE @newDocumentKey uniqueidentifier, @newDocumentVersionKey uniqueidentifier, @documentKey uniqueidentifier

   SET @newDocumentKey = NewID()
   SET @newDocumentVersionKey = NewID()

   -- create the UniformRegistry entry first
   INSERT INTO UniformRegistry (UniformKey, ComponentKey)
   SELECT @newDocumentKey, ComponentKey
     FROM ComponentRegistry
    WHERE Name = 'Document'
      AND InterfaceName = 'BusinessController'

   -- then the UniformRegistry entry for the version key
   INSERT INTO UniformRegistry (UniformKey, ComponentKey)
   SELECT @newDocumentVersionKey, ComponentKey
     FROM ComponentRegistry
    WHERE Name = 'DocumentVersion'
      AND InterfaceName = 'BusinessController'

   -- get the latest document key
   exec asi_DocumentGetLatestKey @key, @documentKey OUT

   -- then create the copy
   INSERT INTO DocumentMain (
          DocumentKey,
          DocumentTypeCode,
          DocumentName,
          DocumentVersionKey,
          DocumentStatusCode,
          DocumentCode,
          AlternateName,
          Blob,
          IsSystem,
          AccessKey,
          DefaultChildAccessKey,
          DocumentDescription,
          ContainsChildrenFlag,
          BranchedFromDocumentKey,
          RelatedDocumentVersionKey,
          StatusUpdatedByUserKey,
          StatusUpdatedOn,
          UpdatedByUserKey,
          UpdatedOn,
          CreatedByUserKey,
          CreatedOn)
   SELECT @newDocumentKey,
          a.DocumentTypeCode,
          ISNULL(@documentName, a.DocumentName),
          @newDocumentVersionKey,
          CASE b.TrackVersionsFlag WHEN 0 THEN 40 ELSE 10 END,
          a.DocumentCode,
          a.AlternateName,
          a.Blob,
          0, -- a.IsSystem, -- Creating a copy of a system item should not be automatically set to IsSystem=true
          a.AccessKey,
          a.DefaultChildAccessKey,
          a.DocumentDescription,
          a.ContainsChildrenFlag,
          a.DocumentKey,
          a.RelatedDocumentVersionKey,
          @userKey,
          GetDate(),
          @userKey,
          GetDate(),
          @userKey,
          GetDate()
     FROM DocumentMain a inner join DocumentTypeRef b on a.DocumentTypeCode = b.DocumentTypeCode
    WHERE a.DocumentKey = @documentKey
      AND EXISTS(
          SELECT 1
            FROM AccessItem INNER JOIN UserToken ON AccessItem.Grantee = UserToken.Grantee OR AccessItem.Grantee = @loggedInUserGroupKey
           WHERE AccessItem.AccessKey = a.AccessKey
             AND UserToken.UserKey = @userKey
             AND (AccessItem.Permission&3)>0)

   -- link the new copy into the specified folder
   exec asi_DocumentLinkDocument @folderHierarchyKey, @newDocumentVersionKey
END



GO
Uses